<?php

/**
 * Agent
 */
class Controller_Agent extends Controller_Abstract
{
    function actionIndex()
    {
       
    }

	
    function actionList(){
    
    	$page = $this->_context->param('page',1);
    	$page_size = $this->_context->param('row',10);
    	
    	if($this->_context->query('site_id')){
    		$conditons='site_id='.$this->_context->get('site_id');
    	}
    	$list = Agent::find($conditons)->asArray()->limitPage($page, $page_size)->getAll();
    	
    	$rowset = site::find("status=0")->asArray()->getAll();
    	$site=Helper_Array::toHashmap($rowset, 'site_id','name');
    	
    	$result=array();
    	if($list){
    		foreach($list as $key => $val){
    			$row=$val;
    			$row['site']=$site[$val['site_id']];
    			$result[]=$row;
    		}
    	}
    	return json_encode($result);
    
    }
    
    function actionNew(){
    	
    	$agent = new Agent($_POST);
    	
	    if ($agent->save()){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('errorMsg'=>'系统出错,操作未能完成.'));
		}
    }
    function actionUpdate(){
    	$id=$this->_context->post('agent_id');
    	$agent = Agent::find('agent_id=?',$id)->getOne();
    	
    	$agent->changeProps($_POST);
    	
    	try{
    		$agent->save();
    		return  json_encode(array('success'=>true));
    	}
    	catch (Exception $e){
    		return json_encode(array('errorMsg'=>'系统出错,操作未能完成.'));
    	}
    }
    
    function actionDestroy(){
    	
    	$id=$this->_context->post('id',0);
    	$row=Agent::find("agent_id=?",$id)->getOne();

    	try {
    		$row->destroy();
			return  json_encode(array('success'=>true));
    	}
    	catch (Exception $e){
				return json_encode(array('errorMsg'=>'系统出错,操作未能完成.'));
		}
    }
}

